<template>
  <van-button
    :loading="loading"
    style="border:none;width:40px;padding: 0"
    @click.prevent="changeLiked"
  >
    <van-icon
      :class="value===1?'liked':''"
      color="#777"
      :name="value===1?'good-job':'good-job-o'"
    />
  </van-button>
</template>

<script>
import { likedArticle, deleteLiked } from '@/api/article.js'
export default {
  props: {
    value: {
      required: true
    },
    art_id: {
      type: [Object, Number, String],
      required: true
    }
  },
  data () {
    return {
      loading: false
    }
  },
  methods: {
    async  changeLiked () {
      this.loading = true
      try {
        if (this.value === 1) {
          await deleteLiked(this.art_id)
          this.$emit('input', 0)
          this.$toast.success('取消点赞成功')
        } else {
          await likedArticle(this.art_id)
          this.$emit('input', 1)
          this.$toast.success('点赞成功')
        }
      } catch (err) {
        if (err.response && err.response.status === 401) {
          this.loading = false
          return this.$toast('请先登录')
        }
        this.$toast('操作失败，请稍后重试')
      }
      this.loading = false
    }
  }
}
</script>

<style>
.liked {
  color: #e5645f !important;
}
.van-icon {
  font-size: 40px;
}
</style>
